package apis

import (
	"github.com/gin-gonic/gin"
	"fmt"
	"net/http"
	"hulk-api/models/server/ansible"
)

func GetAnsibeHosts(c *gin.Context) {
	var hg ansible.HostGroup
	hosts,_ := hg.GetHosts()
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"hosts": hosts,
	})

}

func GetIplists(c *gin.Context) {
	var ip ansible.IpAddress
	//c.BindJSON(&u)   //TODO：ROLE 鉴权

	ips,_ := ip.GetLists()
	var iplists []string
	for _,v := range ips{
		iplists = append(iplists,v.Ip)
	}
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"iplists": iplists,
	})

}

func UpdateHostGroup(c *gin.Context) {
	var h ansible.HostGroup
	c.BindJSON(&h)   //TODO：ROLE 鉴权

	h.UpdateHosts()

	c.JSON(http.StatusOK,gin.H{
		"code": 200,
	})

}
func AddHostGroup(c *gin.Context) {
	var h ansible.HostGroup
	c.BindJSON(&h)
	err := h.Add()
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code": 400,
			"message": err.Error() ,
		})
	} else {
		c.JSON(http.StatusOK,gin.H{
			"code": 200,
		})
	}
}

func GetTemplatePath(c *gin.Context) {
	var tem ansible.TemplatePath
	tem.Name = c.Query("name")
	fmt.Println("path name is .. ." , tem.Name)
	paths,err := tem.GetPath()
	if err != nil {
		fmt.Println("error is",err)
	}
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"path": paths,
	})

}




func RunAnsibleCMD(c *gin.Context) {
	var module ansible.Module
	c.BindJSON(&module)
	result := module.Run()
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"result": result,
	})
}

func RunAnsiblePlaybook(c *gin.Context) {
	var playbook ansible.Playbook
	c.BindJSON(&playbook)

	result := playbook.InitSetting().Run()
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"result": result,
	})
}